<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>emailaddress.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_format_emailaddress.js.html">emailaddress.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides functions to parse and manipulate email addresses.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.format.EmailAddress&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.string&#39;);
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>
<a name="line26"></a>/**
<a name="line27"></a> * Formats an email address string for display, and allows for extraction of
<a name="line28"></a> * The individual componants of the address.
<a name="line29"></a> * @param {string=} opt_address The email address.
<a name="line30"></a> * @param {string=} opt_name The name associated with the email address.
<a name="line31"></a> * @constructor
<a name="line32"></a> */
<a name="line33"></a>goog.format.EmailAddress = function(opt_address, opt_name) {
<a name="line34"></a>  /**
<a name="line35"></a>   * The name or personal string associated with the address.
<a name="line36"></a>   * @type {string}
<a name="line37"></a>   * @private
<a name="line38"></a>   */
<a name="line39"></a>  this.name_ = opt_name || &#39;&#39;;
<a name="line40"></a>
<a name="line41"></a>  /**
<a name="line42"></a>   * The email address.
<a name="line43"></a>   * @type {string}
<a name="line44"></a>   * @private
<a name="line45"></a>   */
<a name="line46"></a>  this.address_ = opt_address || &#39;&#39;;
<a name="line47"></a>};
<a name="line48"></a>
<a name="line49"></a>
<a name="line50"></a>/**
<a name="line51"></a> * Match string for opening tokens.
<a name="line52"></a> * @type {string}
<a name="line53"></a> * @private
<a name="line54"></a> */
<a name="line55"></a>goog.format.EmailAddress.OPENERS_ = &#39;&quot;&lt;([&#39;;
<a name="line56"></a>
<a name="line57"></a>
<a name="line58"></a>/**
<a name="line59"></a> * Match string for closing tokens.
<a name="line60"></a> * @type {string}
<a name="line61"></a> * @private
<a name="line62"></a> */
<a name="line63"></a>goog.format.EmailAddress.CLOSERS_ = &#39;&quot;&gt;)]&#39;;
<a name="line64"></a>
<a name="line65"></a>
<a name="line66"></a>/**
<a name="line67"></a> * A RegExp to check special characters to be quoted.  Used in cleanAddress().
<a name="line68"></a> * @type {RegExp}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.format.EmailAddress.SPECIAL_CHARS_RE_ = /[()&lt;&gt;@,;:\\\&quot;.\[\]]/;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * A RegExp to match all double quotes.  Used in cleanAddress().
<a name="line76"></a> * @type {RegExp}
<a name="line77"></a> * @private
<a name="line78"></a> */
<a name="line79"></a>goog.format.EmailAddress.ALL_DOUBLE_QUOTES_ = /\&quot;/g;
<a name="line80"></a>
<a name="line81"></a>
<a name="line82"></a>/**
<a name="line83"></a> * A RegExp to match escaped double quotes.  Used in parse().
<a name="line84"></a> * @type {RegExp}
<a name="line85"></a> * @private
<a name="line86"></a> */
<a name="line87"></a>goog.format.EmailAddress.ESCAPED_DOUBLE_QUOTES_ = /\\\&quot;/g;
<a name="line88"></a>
<a name="line89"></a>
<a name="line90"></a>/**
<a name="line91"></a> * A RegExp to match all backslashes.  Used in cleanAddress().
<a name="line92"></a> * @type {RegExp}
<a name="line93"></a> * @private
<a name="line94"></a> */
<a name="line95"></a>goog.format.EmailAddress.ALL_BACKSLASHES_ = /\\/g;
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * A RegExp to match escaped backslashes.  Used in parse().
<a name="line100"></a> * @type {RegExp}
<a name="line101"></a> * @private
<a name="line102"></a> */
<a name="line103"></a>goog.format.EmailAddress.ESCAPED_BACKSLASHES_ = /\\\\/g;
<a name="line104"></a>
<a name="line105"></a>
<a name="line106"></a>/**
<a name="line107"></a> * Get the name associated with the email address.
<a name="line108"></a> * @return {string} The name or personal portion of the address.
<a name="line109"></a> */
<a name="line110"></a>goog.format.EmailAddress.prototype.getName = function() {
<a name="line111"></a>  return this.name_;
<a name="line112"></a>};
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>/**
<a name="line116"></a> * Get the email address.
<a name="line117"></a> * @return {string} The email address.
<a name="line118"></a> */
<a name="line119"></a>goog.format.EmailAddress.prototype.getAddress = function() {
<a name="line120"></a>  return this.address_;
<a name="line121"></a>};
<a name="line122"></a>
<a name="line123"></a>
<a name="line124"></a>/**
<a name="line125"></a> * Set the name associated with the email address.
<a name="line126"></a> * @param {string} name The name to associate.
<a name="line127"></a> */
<a name="line128"></a>goog.format.EmailAddress.prototype.setName = function(name) {
<a name="line129"></a>  this.name_ = name;
<a name="line130"></a>};
<a name="line131"></a>
<a name="line132"></a>
<a name="line133"></a>/**
<a name="line134"></a> * Set the email address.
<a name="line135"></a> * @param {string} address The email address.
<a name="line136"></a> */
<a name="line137"></a>goog.format.EmailAddress.prototype.setAddress = function(address) {
<a name="line138"></a>  this.address_ = address;
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Return the address in a standard format:
<a name="line144"></a> *  - remove extra spaces.
<a name="line145"></a> *  - Surround name with quotes if it contains special characters.
<a name="line146"></a> * @return {string} The cleaned address.
<a name="line147"></a> * @override
<a name="line148"></a> */
<a name="line149"></a>goog.format.EmailAddress.prototype.toString = function() {
<a name="line150"></a>  var name = this.getName();
<a name="line151"></a>
<a name="line152"></a>  // We intentionally remove double quotes in the name because escaping
<a name="line153"></a>  // them to \&quot; looks ugly.
<a name="line154"></a>  name = name.replace(goog.format.EmailAddress.ALL_DOUBLE_QUOTES_, &#39;&#39;);
<a name="line155"></a>
<a name="line156"></a>  // If the name has special characters, we need to quote it and escape \&#39;s.
<a name="line157"></a>  var quoteNeeded = goog.format.EmailAddress.SPECIAL_CHARS_RE_.test(name);
<a name="line158"></a>  if (quoteNeeded) {
<a name="line159"></a>    name = &#39;&quot;&#39; +
<a name="line160"></a>        name.replace(goog.format.EmailAddress.ALL_BACKSLASHES_, &#39;\\\\&#39;) + &#39;&quot;&#39;;
<a name="line161"></a>  }
<a name="line162"></a>
<a name="line163"></a>  if (name == &#39;&#39;) {
<a name="line164"></a>    return this.address_;
<a name="line165"></a>  }
<a name="line166"></a>  if (this.address_ == &#39;&#39;) {
<a name="line167"></a>    return name;
<a name="line168"></a>  }
<a name="line169"></a>  return name + &#39; &lt;&#39; + this.address_ + &#39;&gt;&#39;;
<a name="line170"></a>};
<a name="line171"></a>
<a name="line172"></a>
<a name="line173"></a>/**
<a name="line174"></a> * Determines is the current object is a valid email address.
<a name="line175"></a> * @return {boolean} Whether the email address is valid.
<a name="line176"></a> */
<a name="line177"></a>goog.format.EmailAddress.prototype.isValid = function() {
<a name="line178"></a>  return goog.format.EmailAddress.isValidAddrSpec(this.address_);
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Checks if the provided string is a valid email address. Supports both
<a name="line184"></a> * simple email addresses (address specs) and addresses that contain display
<a name="line185"></a> * names.
<a name="line186"></a> * @param {string} str The email address to check.
<a name="line187"></a> * @return {boolean} Whether the provided string is a valid address.
<a name="line188"></a> */
<a name="line189"></a>goog.format.EmailAddress.isValidAddress = function(str) {
<a name="line190"></a>  return goog.format.EmailAddress.parse(str).isValid();
<a name="line191"></a>};
<a name="line192"></a>
<a name="line193"></a>
<a name="line194"></a>/**
<a name="line195"></a> * Checks if the provided string is a valid address spec (local@domain.com).
<a name="line196"></a> * @param {string} str The email address to check.
<a name="line197"></a> * @return {boolean} Whether the provided string is a valid address spec.
<a name="line198"></a> */
<a name="line199"></a>goog.format.EmailAddress.isValidAddrSpec = function(str) {
<a name="line200"></a>  // This is a fairly naive implementation, but it covers 99% of use cases.
<a name="line201"></a>  // For more details, see http://en.wikipedia.org/wiki/Email_address#Syntax
<a name="line202"></a>  // TODO(mariakhomenko): we should also be handling i18n domain names as per
<a name="line203"></a>  // http://en.wikipedia.org/wiki/Internationalized_domain_name
<a name="line204"></a>  var filter =
<a name="line205"></a>      /^[+a-zA-Z0-9_.!#$%&amp;&#39;*\/=?^`{|}~-]+@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,6}$/;
<a name="line206"></a>  return filter.test(str);
<a name="line207"></a>};
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * Parse an email address of the form &quot;name&quot; &amp;lt;address&amp;gt; into
<a name="line212"></a> * an email address.
<a name="line213"></a> * @param {string} addr The address string.
<a name="line214"></a> * @return {goog.format.EmailAddress} The parsed address.
<a name="line215"></a> */
<a name="line216"></a>goog.format.EmailAddress.parse = function(addr) {
<a name="line217"></a>  // TODO(ecattell): Strip bidi markers.
<a name="line218"></a>  var name = &#39;&#39;;
<a name="line219"></a>  var address = &#39;&#39;;
<a name="line220"></a>  for (var i = 0; i &lt; addr.length;) {
<a name="line221"></a>    var token = goog.format.EmailAddress.getToken_(addr, i);
<a name="line222"></a>    if (token.charAt(0) == &#39;&lt;&#39; &amp;&amp; token.indexOf(&#39;&gt;&#39;) != -1) {
<a name="line223"></a>      var end = token.indexOf(&#39;&gt;&#39;);
<a name="line224"></a>      address = token.substring(1, end);
<a name="line225"></a>    } else if (address == &#39;&#39;) {
<a name="line226"></a>      name += token;
<a name="line227"></a>    }
<a name="line228"></a>    i += token.length;
<a name="line229"></a>  }
<a name="line230"></a>
<a name="line231"></a>  // Check if it&#39;s a simple email address of the form &quot;jlim@google.com&quot;.
<a name="line232"></a>  if (address == &#39;&#39; &amp;&amp; name.indexOf(&#39;@&#39;) != -1) {
<a name="line233"></a>    address = name;
<a name="line234"></a>    name = &#39;&#39;;
<a name="line235"></a>  }
<a name="line236"></a>
<a name="line237"></a>  name = goog.string.collapseWhitespace(name);
<a name="line238"></a>  name = goog.string.stripQuotes(name, &#39;\&#39;&#39;);
<a name="line239"></a>  name = goog.string.stripQuotes(name, &#39;&quot;&#39;);
<a name="line240"></a>  // Replace escaped quotes and slashes.
<a name="line241"></a>  name = name.replace(goog.format.EmailAddress.ESCAPED_DOUBLE_QUOTES_, &#39;&quot;&#39;);
<a name="line242"></a>  name = name.replace(goog.format.EmailAddress.ESCAPED_BACKSLASHES_, &#39;\\&#39;);
<a name="line243"></a>  address = goog.string.collapseWhitespace(address);
<a name="line244"></a>  return new goog.format.EmailAddress(address, name);
<a name="line245"></a>};
<a name="line246"></a>
<a name="line247"></a>
<a name="line248"></a>/**
<a name="line249"></a> * Parse a string containing email addresses of the form
<a name="line250"></a> * &quot;name&quot; &amp;lt;address&amp;gt; into an array of email addresses.
<a name="line251"></a> * @param {string} str The address list.
<a name="line252"></a> * @return {Array.&lt;goog.format.EmailAddress&gt;} The parsed emails.
<a name="line253"></a> */
<a name="line254"></a>goog.format.EmailAddress.parseList = function(str) {
<a name="line255"></a>  var result = [];
<a name="line256"></a>  var email = &#39;&#39;;
<a name="line257"></a>  var token;
<a name="line258"></a>
<a name="line259"></a>  for (var i = 0; i &lt; str.length; ) {
<a name="line260"></a>    token = goog.format.EmailAddress.getToken_(str, i);
<a name="line261"></a>    if (token == &#39;,&#39; || token == &#39;;&#39; ||
<a name="line262"></a>        (token == &#39; &#39; &amp;&amp; goog.format.EmailAddress.parse(email).isValid())) {
<a name="line263"></a>      if (!goog.string.isEmpty(email)) {
<a name="line264"></a>        result.push(goog.format.EmailAddress.parse(email));
<a name="line265"></a>      }
<a name="line266"></a>      email = &#39;&#39;;
<a name="line267"></a>      i++;
<a name="line268"></a>      continue;
<a name="line269"></a>    }
<a name="line270"></a>    email += token;
<a name="line271"></a>    i += token.length;
<a name="line272"></a>  }
<a name="line273"></a>
<a name="line274"></a>  // Add the final token.
<a name="line275"></a>  if (!goog.string.isEmpty(email)) {
<a name="line276"></a>    result.push(goog.format.EmailAddress.parse(email));
<a name="line277"></a>  }
<a name="line278"></a>  return result;
<a name="line279"></a>};
<a name="line280"></a>
<a name="line281"></a>
<a name="line282"></a>/**
<a name="line283"></a> * Get the next token from a position in an address string.
<a name="line284"></a> * @param {string} str the string.
<a name="line285"></a> * @param {number} pos the position.
<a name="line286"></a> * @return {string} the token.
<a name="line287"></a> * @private
<a name="line288"></a> */
<a name="line289"></a>goog.format.EmailAddress.getToken_ = function(str, pos) {
<a name="line290"></a>  var ch = str.charAt(pos);
<a name="line291"></a>  var p = goog.format.EmailAddress.OPENERS_.indexOf(ch);
<a name="line292"></a>  if (p == -1) {
<a name="line293"></a>    return ch;
<a name="line294"></a>  }
<a name="line295"></a>  if (goog.format.EmailAddress.isEscapedDlQuote_(str, pos)) {
<a name="line296"></a>
<a name="line297"></a>    // If an opener is an escaped quote we do not treat it as a real opener
<a name="line298"></a>    // and keep accumulating the token.
<a name="line299"></a>    return ch;
<a name="line300"></a>  }
<a name="line301"></a>  var closerChar = goog.format.EmailAddress.CLOSERS_.charAt(p);
<a name="line302"></a>  var endPos = str.indexOf(closerChar, pos + 1);
<a name="line303"></a>
<a name="line304"></a>  // If the closer is a quote we go forward skipping escaped quotes until we
<a name="line305"></a>  // hit the real closing one.
<a name="line306"></a>  while (endPos &gt;= 0 &amp;&amp;
<a name="line307"></a>         goog.format.EmailAddress.isEscapedDlQuote_(str, endPos)) {
<a name="line308"></a>    endPos = str.indexOf(closerChar, endPos + 1);
<a name="line309"></a>  }
<a name="line310"></a>  var token = (endPos &gt;= 0) ? str.substring(pos, endPos + 1) : ch;
<a name="line311"></a>  return token;
<a name="line312"></a>};
<a name="line313"></a>
<a name="line314"></a>
<a name="line315"></a>/**
<a name="line316"></a> * Checks if the character in the current position is an escaped double quote
<a name="line317"></a> * ( \&quot; ).
<a name="line318"></a> * @param {string} str the string.
<a name="line319"></a> * @param {number} pos the position.
<a name="line320"></a> * @return {boolean} true if the char is escaped double quote.
<a name="line321"></a> * @private
<a name="line322"></a> */
<a name="line323"></a>goog.format.EmailAddress.isEscapedDlQuote_ = function(str, pos) {
<a name="line324"></a>  if (str.charAt(pos) != &#39;&quot;&#39;) {
<a name="line325"></a>    return false;
<a name="line326"></a>  }
<a name="line327"></a>  var slashCount = 0;
<a name="line328"></a>  for (var idx = pos - 1; idx &gt;= 0 &amp;&amp; str.charAt(idx) == &#39;\\&#39;; idx--) {
<a name="line329"></a>    slashCount++;
<a name="line330"></a>  }
<a name="line331"></a>  return ((slashCount % 2) != 0);
<a name="line332"></a>};
</pre>


</body>
</html>
